package interview.javabase.thread.aqs.semaphore;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;

@Slf4j
@SuppressWarnings("all")
@AllArgsConstructor
class Runner extends Thread {

    private String name;

    private Semaphore semaphore;

    @Override
    public void run() {
        try {
            if (semaphore.tryAcquire()) {
                log.info(System.currentTimeMillis() + "-" + name);
            } else {
                log.info("放弃-" + name);
            }
        } finally {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release();
        }
    }
}